﻿namespace PowerSong.UserInterface
    {
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;

    /// <summary>
    /// Interaction logic for ColorControl.xaml
    /// </summary>
    public partial class ColorControl : UserControl
        {

        #region Brush Property

        public delegate void BrushChangedEventArgs(Brush brush);

        public event BrushChangedEventArgs BrushChanged;

        public static readonly DependencyProperty BrushProperty = DependencyProperty.Register("Brush", typeof(Brush), typeof(ColorControl), new PropertyMetadata(HandleBrushChanged));

        public Brush Brush
            {
            get
                {
                return (Brush)GetValue(BrushProperty);
                }
            set
                {
                SetValue(BrushProperty, value);
                }
            }

        private static void HandleBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
            ((ColorControl)d).Border.Background = (Brush)e.NewValue;
            }

        #endregion

        #region Header Property

        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(string), typeof(ColorControl), new PropertyMetadata(HandleHeaderChanged));

        public string Header
            {
            get
                {
                return (string)GetValue(HeaderProperty);
                }
            set
                {
                SetValue(HeaderProperty, value);
                }
            }

        private static void HandleHeaderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
            ((ColorControl)d).ButtonHeaderTextBlock.Text = (string)e.NewValue;
            }

        #endregion
                
        public Color Color
            {
            get { return ((SolidColorBrush)Brush).Color; }
            }

        public ColorControl()
            {
            InitializeComponent();
            }

        private void Button_Click(object sender, RoutedEventArgs e)
            {

            // Show the dialog
            var color = ((SolidColorBrush)Border.Background).Color;
            var dialog = new System.Windows.Forms.ColorDialog()
                {
                AllowFullOpen = true,
                AnyColor = true,
                FullOpen = true,
                Color = System.Drawing.Color.FromArgb((int)color.ToUInt()),
                SolidColorOnly = true
                };
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {

                // Change the colour
                var newColor = dialog.Color;
                this.Brush = new SolidColorBrush(Color.FromArgb(newColor.A, newColor.R, newColor.G, newColor.B));

                // Activate event
                if (BrushChanged != null) BrushChanged(Border.Background);

                }

            }

        }

    }
